package edu.ccut.saturn.studio.util;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;

public class ConversionUtil {
	
	/**
	 * 把map转成数组
	 * 
	 * */
	static public String[] conversionFromMapToArray(Map<String, String> map){
		Set<String> sets = new HashSet<String>();//得到第一个String
		sets = map.keySet();
		return conversion(sets);
	}
	
	/**
	 * 把list转成数组
	 * 
	 * @param keys
	 * @return
	 */
	static public String[] conversion(List<String> keys) {
		Set<String> set1 = new HashSet<String>();
		set1.addAll(keys);
		String[] ss = new String[set1.size()];
		return ss = (String[]) set1.toArray(ss);
	}

	/**
	 * 将set转换成数组
	 * 
	 * @param set1
	 * @return
	 */
	// TODO:从set返回数组
	static public String[] conversion(Set<String> set1) {
		String[] ss = new String[set1.size()];
		return ss = (String[]) set1.toArray(ss);
	}
	/**
	 * scop域中数值的转换
	 * @param parameterlist
	 */
	static public void scopeMap(List<SaturnParameter> parameterlist) {
		for (int i = 0; i < parameterlist.size(); i++) {
			if (parameterlist.get(i).getScope().equals("application")) {
				parameterlist.get(i).setScope("10");
			}
			if (parameterlist.get(i).getScope().equals("request")) {
				parameterlist.get(i).setScope("2");
			}
			if (parameterlist.get(i).getScope().equals("session")) {
				parameterlist.get(i).setScope("5");
			}
			if (parameterlist.get(i).getScope().equals("logic")) {
				parameterlist.get(i).setScope("1");
			}
			if (parameterlist.get(i).getPassingType().equals(
					SelectionCandidate.passingTypes[0])) {
				parameterlist.get(i).setPassingType("in");
			}
			if (parameterlist.get(i).getPassingType().equals(
					SelectionCandidate.passingTypes[1])) {
				parameterlist.get(i).setPassingType("out");
			}
			if (parameterlist.get(i).getPassingType().equals(
					SelectionCandidate.passingTypes[2])) {
				parameterlist.get(i).setPassingType("in-out");
			}
		}
	}
	
	/**
	 * parameter各参数从xml读到界面
	 * @param parameterlist
	 * */
	static public List<SaturnParameter> scopeMapFromXmlToSurface(List<SaturnParameter> showParameterlist){
		for (int i = 0; i < showParameterlist.size(); i++) {
			if (showParameterlist.get(i).getScope().equals("10")) {
				showParameterlist.get(i).setScope("application");
			}
			if (showParameterlist.get(i).getScope().equals("2")) {
				showParameterlist.get(i).setScope("request");
			}
			if (showParameterlist.get(i).getScope().equals("5")) {
				showParameterlist.get(i).setScope("session");
			}
			if (showParameterlist.get(i).getScope().equals("1")) {
				showParameterlist.get(i).setScope("logic");
			}
			if (showParameterlist.get(i).getPassingType().equals("in")) {
				showParameterlist.get(i).setPassingType(SelectionCandidate.passingTypes[0]);
			}
			if (showParameterlist.get(i).getPassingType().equals("out")) {
				showParameterlist.get(i).setPassingType(SelectionCandidate.passingTypes[1]);
			}
			if (showParameterlist.get(i).getPassingType().equals("in-out")) {
				showParameterlist.get(i).setPassingType(SelectionCandidate.passingTypes[2]);
			}

		}
		return showParameterlist;
	}
	
	/**
	 * 删除parameter后的参数ID排序
	 * */
	static public void reorder(List<SaturnParameter> parameters) {

		if (parameters != null) {
			Collections.sort(parameters);
			for (int i = 0; i < parameters.size(); i++) {
				parameters.get(i).setId("" + (i + 1));
			}
		}
	}
	
	/**
	 * 去除从xml读回所带来的单引号
	 * */
	static public String quotationRemove(String key){
		if(key.length()==0){
			return "";
		}
		int i = 0;
		while ((i = key.indexOf("'")) != -1) {
			key = key.substring(i + 1);
			key = key.substring(0, key.length() - 1);
		}
		return key;
	}
	
	/**
	 * 网页上下文节点转换数据域 alphabet --> digital
	 * @param scope
	 * */
	static public String switchScopeToDigital(String ss){
		if (ss.equals("logic"))
			return "1";
		else if (ss.equals("request"))
			return "2";
		else if (ss.equals("session"))
			return "5";
		else if (ss.equals("application"))
			return "10";
		return "";
	}
	
	/**
	 * 网页上下文节点转换数据域 digital --> alphabet
	 * @param scope
	 * */
	static public String switchScopeToAlphabet(String ss){
		if (ss.equals("1"))
			return "logic";
		else if (ss.equals("2"))
			return "request";
		else if (ss.equals("5"))
			return "session";
		else if (ss.equals("10"))
			return "application";
		return "";
	}
}
